package de.ff7rl.core.gamestate.mainmenu;

import java.awt.Point;
import java.awt.event.KeyEvent;
import java.util.Arrays;

import de.ff7rl.core.TerminalMenu;
import de.ff7rl.core.TerminalMenuItem;
import de.ff7rl.core.gamestate.AbstractGameStateController;
import de.ff7rl.core.gamestate.GameState;

public class MainMenuGameStateController extends AbstractGameStateController {

  private enum MainMenu {
    CONTINUE("Weiter"),

    QUIT("Beenden");

    private final String text;

    private MainMenu(final String theText) {
      text = theText;
    }

    @Override
    public String toString() {
      return text;
    }

  }

  private TerminalMenu mainMenu;

  @Override
  public void enter(final GameState thePreviousGameState, final Object... theArguments) {
    super.enter(thePreviousGameState);
    if (mainMenu == null) {
      mainMenu = new TerminalMenu(game(), new Point(), Arrays.asList(new TerminalMenuItem(MainMenu.CONTINUE),
          new TerminalMenuItem(MainMenu.QUIT)));
    } else {
      mainMenu.write();
    }
  }

  @Override
  public boolean input(final int theKeyCode) {
    switch (theKeyCode) {
      case KeyEvent.VK_UP :
        mainMenu.previous();
        return true;
      case KeyEvent.VK_DOWN :
        mainMenu.next();
        return true;
      case KeyEvent.VK_ENTER :
        switch ((MainMenu) mainMenu.getSelectedItem().getValue()) {
          case CONTINUE :
            game().newGameState(GameState.DUNGEON);
            break;
          case QUIT :
            System.exit(0);
        }
        return true;
    }
    return false;
  }

}
